package com.edwin.tfm;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper{

	 // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "TFMdb";
 
    // Prescription table name
    private static final String TABLE_PRESCRIPTION = "prescription";
 
    // Prescription Table Columns names
    private static final String KEY_PID = "ID";
    private static final String KEY_CLINIC_NAME = "Clinic Name";
    private static final String KEY_PATIENT_NAME = "Patient Name";
    private static final String KEY_ILLNESS = "Illness";
    private static final String KEY_DOCTOR_NAME = "Doctor Name";
    
    // medicine table name
    private static final String TABLE_MEDICINE = "medicine";
 
 // Medicine Table Columns names
    private static final String KEY_MID = "ID";
    private static final String KEY_MED_NAME = "Medicine Name";
    private static final String KEY_MED_TYPE = "Medicine Type";
    private static final String KEY_DAYS = "Days";
    private static final String KEY_TO_TAKE = "To Take";
    private static final String KEY_TAKING_TIME = "Taking Time";
    private static final String KEY_ABFOOD = "After/Before Food";
    // Phone table name
    //private static final String TABLE_PHONE = "phone";
    
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_PRESCRIPTION_TABLE = "CREATE TABLE " + TABLE_PRESCRIPTION + "("
                + KEY_PID + " INTEGER PRIMARY KEY," + KEY_CLINIC_NAME + " TEXT,"
                + KEY_PATIENT_NAME + " TEXT" + KEY_ILLNESS + " TEXT,"
                + KEY_DOCTOR_NAME + " TEXT)";
        db.execSQL(CREATE_PRESCRIPTION_TABLE);
        
        String CREATE_MEDICINE_TABLE = "CREATE TABLE " + TABLE_MEDICINE + "("
                + KEY_MID + " INTEGER PRIMARY KEY," + KEY_MED_NAME + " TEXT,"
                + KEY_MED_TYPE + " TEXT" + KEY_DAYS + " TEXT," + KEY_TO_TAKE + " TEXT,"
                + KEY_TAKING_TIME + " TEXT," + KEY_ABFOOD + " TEXT)";
        db.execSQL(CREATE_MEDICINE_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRESCRIPTION);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEDICINE);
 
        // Create tables again
        onCreate(db);
    }
 
	    // Getting All Prescriptions
	 public List<PrescriptionDB> getAllPrescription() {
	    List<PrescriptionDB> prescriptionList = new ArrayList<PrescriptionDB>();
	    // Select All Query
	    String selectQuery = "SELECT  * FROM " + TABLE_PRESCRIPTION;
	 
	    SQLiteDatabase db = this.getWritableDatabase();
	    Cursor cursor = db.rawQuery(selectQuery, null);
	 
	    // looping through all rows and adding to list
	    if (cursor.moveToFirst()) {
	        do {
	            PrescriptionDB p = new PrescriptionDB(0, selectQuery, selectQuery, selectQuery, selectQuery);
	            p.setID(Integer.parseInt(cursor.getString(0)));
	            p.setClinicName(cursor.getString(1));
	            p.setPatientName(cursor.getString(2));
	            p.setIllness(cursor.getString(3));
	            p.setDoctorName(cursor.getString(4));
	            // Adding contact to list
	            prescriptionList.add(p);
	        } while (cursor.moveToNext());
	    }
	 
	    // return prescription list
	    return prescriptionList;
	}
	 
	   // Getting All Prescriptions
		 public List<MedicineDB> getAllmedicine() {
		    List<MedicineDB> medicineList = new ArrayList<MedicineDB>();
		    // Select All Query
		    String selectQuery = "SELECT  * FROM " + TABLE_MEDICINE;
		 
		    SQLiteDatabase db = this.getWritableDatabase();
		    Cursor cursor = db.rawQuery(selectQuery, null);
		 
		    // looping through all rows and adding to list
		    if (cursor.moveToFirst()) {
		        do {
		            MedicineDB m = new MedicineDB(0, selectQuery, selectQuery, selectQuery, selectQuery, 0, selectQuery);
		            m.setMID(Integer.parseInt(cursor.getString(0)));
		            m.setMedName(cursor.getString(1));
		            m.setMedType(cursor.getString(2));
		            m.setDays(cursor.getString(3));
		            m.setToTake(cursor.getString(4));
		            m.setTakingTime(Integer.parseInt(cursor.getString(5)));
		            m.setAbFood(cursor.getString(6));
		            // Adding contact to list
		            medicineList.add(m);
		        } while (cursor.moveToNext());
		    }
		 
		    // return prescription list
		    return medicineList;
		}
		 
		// Adding new contact
		    void addPrescription(PrescriptionDB p) {
		        SQLiteDatabase db = this.getWritableDatabase();
		 
		        ContentValues values = new ContentValues();
		        values.put(KEY_PID, p.getPID()); // Contact ID
		        values.put(KEY_CLINIC_NAME, p.getClinicName()); // Clinic Name
		        values.put(KEY_PATIENT_NAME, p.getPatientName());
		        values.put(KEY_ILLNESS, p.getIllness());
		        values.put(KEY_DOCTOR_NAME, p.getDoctorName());
		        
		        // Inserting Row
		        db.insert(TABLE_PRESCRIPTION, null, values);
		        db.close(); // Closing database connection
		    }
		 
		    // Getting single contact
		    PrescriptionDB getPrescription(int id) {
		        SQLiteDatabase db = this.getReadableDatabase();
		 
		        Cursor cursor = db.query(TABLE_PRESCRIPTION, new String[] { KEY_PID,
		                KEY_CLINIC_NAME, KEY_PATIENT_NAME, KEY_ILLNESS, KEY_DOCTOR_NAME }, KEY_PID + "=?",
		                new String[] { String.valueOf(id) }, null, null, null, null);
		        if (cursor != null)
		            cursor.moveToFirst();
		 
		        PrescriptionDB plist = new PrescriptionDB(Integer.parseInt(cursor.getString(0)),
		                cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
		        // return prescription
		        return plist;
		    }
		    
		 // Adding new medicine
		    void addMedicine(MedicineDB m) {
		        SQLiteDatabase db = this.getWritableDatabase();
		 
		        ContentValues values = new ContentValues();
		        values.put(KEY_MID, m.getMID()); 
		        values.put(KEY_MED_NAME, m.getMedName()); 
		        values.put(KEY_MED_TYPE, m.getMedType()); 
		        values.put(KEY_DAYS, m.getDays()); 
		        values.put(KEY_TO_TAKE, m.getToTake()); 
		        values.put(KEY_TAKING_TIME, m.getTakingTime()); 
		        values.put(KEY_ABFOOD, m.getAbFood());
		        
		        // Inserting Row
		        db.insert(TABLE_MEDICINE, null, values);
		        db.close(); // Closing database connection
		    }
		 
		    // Getting single medicine
		    MedicineDB getMedicine(int id) {
		        SQLiteDatabase db = this.getReadableDatabase();
		 
		        Cursor cursor = db.query(TABLE_MEDICINE, new String[] { KEY_MID,
		                KEY_MED_NAME, KEY_MED_TYPE, KEY_DAYS, KEY_TO_TAKE, KEY_TAKING_TIME, KEY_ABFOOD }, KEY_MID + "=?",
		                new String[] { String.valueOf(id) }, null, null, null, null);
		        if (cursor != null)
		            cursor.moveToFirst();
		 
		        MedicineDB mlist = new MedicineDB (Integer.parseInt(cursor.getString(0)),
		                cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Integer.parseInt(cursor.getString(5)), cursor.getString(6));
		        // return medicine
		        return mlist;
		    }
}
